local skel = fk.CreateSkill {
  name = "efengqi__zhiba",
}

Fk:loadTranslationTable{
  ["efengqi__zhiba"] = "制霸",
  [":efengqi__zhiba"] = "若你：有手牌，你与其他角色可以跳过摸牌阶段与对方拼点，赢者摸三张牌；无手牌，一名角色可以跳过弃牌阶段令你摸三张牌。",

  ["#efengqi__zhiba-skipDiscard"] = "制霸：你可以跳过弃牌阶段，令 %src 摸三张牌。",
  ["#efengqi__zhiba-choose"] = "制霸：你可以跳过摸牌阶段，与一名角色拼点，赢者摸三张牌",
  ["#efengqi__zhiba-skipDraw"] = "制霸：你可以跳过摸牌阶段，与 %src 拼点，赢者摸三张牌",

  ["$efengqi__zhiba1"] = "我若怕你，非孙伯符也！",
  ["$efengqi__zhiba2"] = "是友是敌，一探便知。",
}

skel:addEffect(fk.EventPhaseChanging, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or data.skipped then return false end
    if data.phase == Player.Discard then
      return player:isKongcheng()
    elseif data.phase == Player.Draw then
      if target == player then
        return table.find(player.room.alive_players, function (p)
          return player:canPindian(p)
        end) ~= nil
      else
        return target:canPindian(player)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if data.phase == Player.Discard then
      if player.room:askToSkillInvoke(target, { skill_name = skel.name, prompt = "#efengqi__zhiba-skipDiscard:"..player.id}) then
        event:setCostData(self, {tos = {target}})
        return true
      end
    else
      if target == player then
        local targets = table.filter(room:getOtherPlayers(player, false), function (p) return player:canPindian(p) end)
        if #targets == 0 then return false end
        local tos = player.room:askToChoosePlayers(player, { targets = targets, min_num = 1, max_num = 1,
        prompt = "#efengqi__zhiba-choose", skill_name = skel.name})
        if #tos > 0 then
          event:setCostData(self, {tos = tos})
          return true
        end
      else
        if player.room:askToSkillInvoke(target, { skill_name = skel.name, prompt = "#efengqi__zhiba-skipDraw:"..player.id}) then
          event:setCostData(self, {tos = {target}})
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    data.skipped = true
    if data.phase == Player.Discard then
      player:drawCards(3, skel.name)
    else
      local from, to
      if target == player then
        from, to = player, event:getCostData(self).tos[1]
      else
        from, to = target, player
      end
      local pindian = from:pindian({to}, skel.name)
      local winner = pindian.results[to].winner
      if winner and winner:isAlive() then
        winner:drawCards(3, skel.name)
      end
    end
    --target:skip(data.phase)
  end,
})

return skel
